package com.ficus.road.maintain.third.zh.maintenance;

import com.alibaba.fastjson.JSONObject;
import com.ficus.road.maintain.core.model.zh.BasicBridgeZH;
import com.ficus.road.maintain.core.model.zh.BridgeInfo;
import com.ficus.road.maintain.dao.BridgeInfoDao;
import com.ficus.road.maintain.third.zh.BaseZH;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * @author xiawen yang
 * @date 2022/4/1 下午2:52
 */
@Component
public class ZHBasicBridge extends BaseZH {

    @Autowired
    private BridgeInfoDao bridgeInfoDao;

    @PostConstruct
    void init() {
        arg0 = "getBirdgeBasicinfo";
        arg1 = "{\"type\":\"all\"}";
        errMsg = "调用中海接口获取桥梁基础数据异常!";
        successMsg = "调用中海接口获取桥梁基础数据成功!";
    }

    //    @Scheduled(cron = "0/5 * * * * ?", zone = "GMT+8:00")
    @Scheduled(cron = "0 0 12,14,16 * * ?")
    public void refreshBasicBridge() {
        init();
        String basicBridgeStr = null;
        int i = 0;
        while (i < 10) {
            basicBridgeStr = getZHData();
            if (ObjectUtils.isNotEmpty(basicBridgeStr)) {
                break;
            }
            i++;
        }
        if (ObjectUtils.isEmpty(basicBridgeStr)) {
            return;
        }
        BasicBridgeZH basicBridgeZH = JSONObject.parseObject(basicBridgeStr, BasicBridgeZH.class);
        List<BridgeInfo> bridgeInfoList = basicBridgeZH.convert2BridgeInfo(basicBridgeZH);
        bridgeInfoList.parallelStream().forEach(item -> {
            bridgeInfoDao.saveOrUpdate(item);
        });
    }
}
